//
//  Visitor_waitHandleView.m
//  FaceRecognition
//
//  Created by zz on 17/7/5.
//  Copyright © 2017年 Sylar. All rights reserved.
//

#import "Visitor_waitHandleView.h"

@interface Visitor_waitHandleView() <UITextViewDelegate>


@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *nameLabel; //访客姓名
@property (nonatomic, strong) UILabel *visitedLabel; //被访问者
@property (nonatomic, strong) UILabel *startTimeLabel; //访问开始时间
@property (nonatomic, strong) UILabel *endTimeLabel; //访问结束时间
@property (nonatomic, strong) UILabel *visitorStateLabel; //状态

@property (nonatomic, strong) UILabel *visitorSexLabel; //访客性别
@property (nonatomic, strong) UILabel *visitorUnitLabel; //访客单位
@property (nonatomic, strong) UILabel *phoneLabel; //访客电话

@property (nonatomic, strong) UILabel *visitorTimeLabel; //访问时间

@property (nonatomic, strong) UILabel *visitorLengthLabel; //访问时长

@property (nonatomic, strong) UILabel *visitorLeaveMsgLabel; //访客留言

@property (nonatomic, strong) UILabel *textPldLabel;

@property (nonatomic, strong) UITextView *textView;

@property (nonatomic, strong) UIButton *closeBtn;

@property (nonatomic, assign) CGFloat bgviewOriginY;
@property (nonatomic, assign) CGFloat closeBtnOriginY;

@end

@implementation Visitor_waitHandleView {
    
    CGFloat bottomMargin; //bgview距下部距离
    
    BOOL keyboardShow;
}


- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeBtn setImage:[UIImage imageNamed:@"fangke_close"] forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(tapToHidde) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_closeBtn];
        
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [UIButton buttonWithType:UIButtonTypeSystem];
        
        CGFloat x = frame.size.width;
        CGFloat h = frame.size.height;
        
        UITapGestureRecognizer *hiddeTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToHidde)];
        [self addGestureRecognizer:hiddeTap];
        
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(33, (h - 416) * 0.5, x - 33 * 2, 466)];
        _bgView.backgroundColor = [UIColor whiteColor];
    
        UITapGestureRecognizer *resignKeyboardTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToResignKeyboard)];
        [_bgView addGestureRecognizer:resignKeyboardTap];
        
        [self addSubview:_bgView];
        bottomMargin = frame.size.height - CGRectGetMaxY(_bgView.frame);
        
        _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(16, 28, 100, 100)];
        _imgView.contentMode = UIViewContentModeScaleAspectFill;
        _imgView.clipsToBounds = YES;
        [_bgView addSubview:_imgView];
        
        
        x = CGRectGetMaxX(_imgView.frame);
        
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(x + 21, 36, 200, 16)];
        _nameLabel.font = [UIFont systemFontOfSize:16];
        _nameLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _nameLabel.text = @"访客姓名: 王新山";
        [_bgView addSubview:_nameLabel];
        
        _visitorSexLabel = [[UILabel alloc] initWithFrame:CGRectMake(x + 21, CGRectGetMaxY(_nameLabel.frame) + 18, 200, 16)];
        _visitorSexLabel.font = [UIFont systemFontOfSize:16];
        _visitorSexLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _visitorSexLabel.text = @"访客性别: 男";
        _visitorSexLabel.hidden = YES;
        [_bgView addSubview:_visitorSexLabel];

        _visitorStateLabel = [[UILabel alloc] initWithFrame:CGRectMake(x + 21, CGRectGetMaxY(_visitorSexLabel.frame) + 18, 200, 16)];
        _visitorStateLabel.font = [UIFont systemFontOfSize:16];
        _visitorStateLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"访问状态:待处理"];
        [attrStr addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#f55f5f"]} range:NSMakeRange(5, 3)];
        _visitorStateLabel.attributedText = attrStr;
        [_bgView addSubview:_visitorStateLabel];
        
        _visitorUnitLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(_imgView.frame) + 22, 200, 16)];
        _visitorUnitLabel.font = [UIFont systemFontOfSize:16];
        _visitorUnitLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _visitorUnitLabel.text = @"访客单位:新华区教育局";
        [_bgView addSubview:_visitorUnitLabel];

        
        _phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(_visitorUnitLabel.frame) + 16, 200, 16)];
        _phoneLabel.font = [UIFont systemFontOfSize:16];
        _phoneLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _phoneLabel.text = @"联系方式:12345678901";
        [_bgView addSubview:_phoneLabel];
        
        
        _visitorTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(_phoneLabel.frame) + 16, 250, 16)];
        _visitorTimeLabel.font = [UIFont systemFontOfSize:16];
        _visitorTimeLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _visitorTimeLabel.text = @"访问时间:2017-2018";
        [_bgView addSubview:_visitorTimeLabel];
        
        _visitorLengthLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(_visitorTimeLabel.frame) + 16, 200, 16)];
        _visitorLengthLabel.font = [UIFont systemFontOfSize:16];
        _visitorLengthLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _visitorLengthLabel.text = @"访问时长:4";
        [_bgView addSubview:_visitorLengthLabel];
        
        _visitorLeaveMsgLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(_visitorLengthLabel.frame) + 16, 200, 16)];
        _visitorLeaveMsgLabel.font = [UIFont systemFontOfSize:16];
        _visitorLeaveMsgLabel.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _visitorLeaveMsgLabel.text = @"访客留言:去年买了个表";
        [_bgView addSubview:_visitorLeaveMsgLabel];
        
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(_visitorLeaveMsgLabel.frame) + 18, frame.size.width - (16 + 33) * 2, 60)];
        _textView.layer.borderWidth = 0.5;
        _textView.delegate = self;
        _textView.layer.borderColor = [UIColor colorWithHexString:@"#979797"].CGColor;
        [_bgView addSubview:_textView];
        
        _agreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_agreeBtn setTitle:@"同意" forState:UIControlStateNormal];
        [_agreeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_agreeBtn addTarget:self action:@selector(agreeAction:) forControlEvents:UIControlEventTouchUpInside];
        [_agreeBtn setBackgroundColor:[UIColor colorWithHexString:@"#45abf9"]];
        
        _agreeBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        _agreeBtn.layer.cornerRadius = 4;
        _agreeBtn.frame = CGRectMake(28, CGRectGetMaxY(_textView.frame) + 18, 100, 40);
        [_bgView addSubview:_agreeBtn];
        
        
        _refuseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_refuseBtn setTitle:@"拒绝" forState:UIControlStateNormal];
        [_refuseBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_refuseBtn addTarget:self action:@selector(refuseAction:) forControlEvents:UIControlEventTouchUpInside];
        [_refuseBtn setBackgroundColor:[UIColor colorWithHexString:@"#f55f5f"]];
        
        _refuseBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        _refuseBtn.layer.cornerRadius = 4;
        _refuseBtn.frame = CGRectMake(CGRectGetMaxX(_agreeBtn.frame)+53, CGRectGetMaxY(_textView.frame) + 18, 100, 40);
        [_bgView addSubview:_refuseBtn];
        
        _textPldLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 8, 200, 15)];
        _textPldLabel.font = [UIFont systemFontOfSize:15];
        _textPldLabel.textColor = [UIColor colorWithHexString:@"#b9b9b9"];
        _textPldLabel.text = @"受访留言:";
        _textView.delegate = self;
        [_textView addSubview:_textPldLabel];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHidde:) name:UIKeyboardWillHideNotification object:nil];
        
        
        keyboardShow = false;
    }
    
    return self;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    _closeBtn.frame = CGRectMake(0, 0, 28, 28);
    _closeBtn.center = CGPointMake(CGRectGetMaxX(_bgView.frame), CGRectGetMinY(_bgView.frame));
    [self bringSubviewToFront:_closeBtn];
    
    _bgviewOriginY = self.bgView.y;
    _closeBtnOriginY = _closeBtn.origin.y;
}


#pragma mark -Notification
- (void)keyboardShow:(NSNotification *)noti {
    
    CGRect rect = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    _bgView.y = _bgView.y - rect.size.height + bottomMargin;
    
    
    
    _closeBtn.y = _closeBtn.y - rect.size.height + bottomMargin;
    
    bottomMargin = self.height - CGRectGetMaxY(_bgView.frame);
    
    keyboardShow = YES;
}

- (void)keyboardHidde:(NSNotification *)noti {
    
//    CGRect rect = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//    _bgView.y = _bgView.y + rect.size.height - bottomMargin;
//    
//    _closeBtn.y = _closeBtn.y + rect.size.height - bottomMargin;
    
    _bgView.y = _bgviewOriginY;
    _closeBtn.y = _closeBtnOriginY;
    bottomMargin = self.height - CGRectGetMaxY(_bgView.frame);
}

#pragma mark -Action
- (void)tapToResignKeyboard {
 
    [self endEditing:YES];
    
}

- (void)tapToHidde {
    
    if (keyboardShow == YES) {
        
        keyboardShow = NO;
        [self endEditing:YES];
    }else {
        
        [self removeFromSuperview];
    }
    
    
    
}

//同意
- (void)agreeAction:(UIButton *)sender {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@(_model.id) forKey:@"id"];
    [dict setObject:@(_model.intervieweeid) forKey:@"intervieweeid"];
    if (_textView.text.length > 0) {
        
        [dict setObject:_textView.text forKey:@"message"];
    }
    [dict setObject:SCHOOLID forKey:@"scid"];
    
    [Utils Java_get:@"pad/agreeVisitor" parameters:dict success:^(NSInteger recode, id jsonObject) {
        
        _model.approval = 1;
        if (_delegate && [_delegate respondsToSelector:@selector(handledAction:)]) {
            
            [_delegate handledAction:_model];
        }
        [self removeFromSuperview];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        
        
    }];
    
}

//拒绝
- (void)refuseAction:(UIButton *)sender {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@(_model.id) forKey:@"id"];
    [dict setObject:@(_model.intervieweeid) forKey:@"intervieweeid"];
    if (_textView.text.length > 0) {
        
        [dict setObject:_textView.text forKey:@"message"];
    }
    [dict setObject:SCHOOLID forKey:@"scid"];
    
    [Utils Java_get:@"pad/disagreeVisitor" parameters:dict success:^(NSInteger recode, id jsonObject) {
        
        _model.approval = 2;
        if (_delegate && [_delegate respondsToSelector:@selector(handledAction:)]) {
            
            [_delegate handledAction:_model];
        }
        [self removeFromSuperview];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        
        
    }];

}

#pragma mark -UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    
    _textPldLabel.hidden = YES;
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {

    _textPldLabel.hidden = textView.text.length != 0;
    if (textView.text.length > 60) {
        
        textView.text = [textView.text substringToIndex:60];
    }
}



- (void)setModel:(FR_VisitorModel *)model {
    
    _model = model;
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",REQUESTIMAGEURL,model.photo];
    [_imgView sd_setImageWithURL:[NSURL URLWithString:urlStr]];
    
    _nameLabel.text = [NSString stringWithFormat:@"访客姓名:%@",model.name];
    
    _visitorUnitLabel.text = [NSString stringWithFormat:@"访客单位:%@",model.comefrom];
    _phoneLabel.text = [NSString stringWithFormat:@"联系方式:%@",model.telephone];
    _visitorLengthLabel.text = [NSString stringWithFormat:@"访问时长:%@",model.timelength];
    _visitorTimeLabel.text = [NSString stringWithFormat:@"访问时间:%@",model.starttime];
    _visitorLeaveMsgLabel.text = [NSString stringWithFormat:@"访客留言:%@",model.remark];
}

@end
